//
//  ADJgSDKRewardVodAdAdapter.m
//  AnyThinkSDKDemo
//
//  Created by 刘娟 on 2024/9/25.
//  Copyright © 2024 抽筋的灯. All rights reserved.
//

#import "ADJgSDKRewardVodAdAdapter.h"
#import "ADJgSDKRewardVodAdContainer.h"
#import "ADJgSDKRewardVodAdCustomEvent.h"
#import "ATADJgSDKBiddingManager.h"
#import "ATADJgSDKBiddingRequest.h"
@interface ADJgSDKRewardVodAdAdapter()

@property(nonatomic, readonly)ADJgSDKRewardVodAdCustomEvent *customEvent;
@property(nonatomic, strong)ADJgSDKRewardVodAdContainer *rewardVodAdContainer;

@end

@implementation ADJgSDKRewardVodAdAdapter

+(BOOL)adReadyWithCustomObject:(id)customObject info:(NSDictionary*)info {
    //极光SDK没有是否有效字段 默认有效
    return YES;
}

+(void)showRewardedVideo:(ATRewardedVideo*)rewardedVideo inViewController:(UIViewController*)viewController delegate:(id<ATRewardedVideoDelegate>)delegate {
    ADJgSDKRewardVodAdCustomEvent *customEvent = (ADJgSDKRewardVodAdCustomEvent*)rewardedVideo.customEvent;
    customEvent.delegate = delegate;
    [customEvent.expressRvAdContainer showRewardVodAdWithRootViewController:viewController];
}

-(instancetype) initWithNetworkCustomInfo:(NSDictionary*)serverInfo localInfo:(NSDictionary*)localInfo {
    self = [super init];
    if (self != nil) {
#if DEBUG
        NSLog(@"ADJgSDK - appid:%@",serverInfo[@"app_id"]);
#endif
        
        if (![[ATAPI sharedInstance] initFlagForNetwork:@"ADJgSDK"]) {
            [[ATAPI sharedInstance] setInitFlagForNetwork:@"ADJgSDK"];
            [ADJgSDK initWithAppId:serverInfo[@"app_id"] completionBlock:^(NSError * _Nullable error) {
                if (error) {
                    [[ATAPI sharedInstance] setInitFlag:0 forNetwork:@"ADJgSDK"];
                    NSLog(@"ADJgSDK - init failed:%@",error);
                }else{
                    NSLog(@"ADJgSDK - init success");
                }
            }];
            
        }
       
    }
    return self;
}

-(void) loadADWithInfo:(NSDictionary*)serverInfo localInfo:(NSDictionary*)localInfo completion:(void (^)(NSArray<NSDictionary *> *, NSError *))completion {
    dispatch_async(dispatch_get_main_queue(), ^{
#if DEBUG
        NSLog(@"ADJgSDK - serverInfo:%@\nlocalInfo%@",serverInfo,localInfo);
#endif
        self->_customEvent = [[ADJgSDKRewardVodAdCustomEvent alloc] initWithInfo:serverInfo localInfo:localInfo];
        self->_customEvent.requestCompletionBlock = completion;
        
        NSString *bidId = serverInfo[kATAdapterCustomInfoBuyeruIdKey];
        if (bidId.length) {
            
            ATADJgSDKBiddingRequest *request = [[ATADJgSDKBiddingManager sharedInstance] getRequestItemWithUnitID:serverInfo[@"slot_id"]];
            
            if (request.customObject!=nil) { // load secced
                self.rewardVodAdContainer = request.customObject;
                self.rewardVodAdContainer.delegate = self->_customEvent;
//                self.rewardVodAdContainer.containerDelegate = self->_customEvent;
                self->_customEvent.expressRvAdContainer = self.rewardVodAdContainer;
                [self->_customEvent trackRewardedVideoAdLoaded:self.rewardVodAdContainer adExtra:nil];
            } else { // fail
                NSError *error = [NSError errorWithDomain:ATADLoadingErrorDomain code:-1000001 userInfo:@{NSLocalizedDescriptionKey:@"AT has failed to load intertitial.", NSLocalizedFailureReasonErrorKey:@"It took too long to load placement stragety."}];
                [self->_customEvent trackRewardedVideoAdLoadFailed:error];
            }
            [[ATADJgSDKBiddingManager sharedInstance] removeRequestItmeWithUnitID:serverInfo[@"slot_id"]];
        } else {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self->_customEvent loadRawardVodWithPosId:serverInfo[@"slot_id"]];

            });
        }
    });
}
#pragma mark - Header bidding
#pragma mark - c2s
+(void)bidRequestWithPlacementModel:(ATPlacementModel*)placementModel unitGroupModel:(ATUnitGroupModel*)unitGroupModel info:(NSDictionary*)info completion:(void(^)(ATBidInfo *bidInfo, NSError *error))completion {

    NSLog(@"%s", __FUNCTION__);
    if (NSClassFromString(@"ADJgSDK") == nil) {
        if (completion != nil) { completion( nil, [NSError errorWithDomain:@"com.ubix.mediation.ios" code:1 userInfo:@{NSLocalizedDescriptionKey:@"Bid request has failed", NSLocalizedFailureReasonErrorKey:@"ADJgSDK is not imported"}]); }
        return;
    }
    
    if (![[ATAPI sharedInstance] initFlagForNetwork:@"ADJgSDK"]) {
        [[ATAPI sharedInstance] setInitFlagForNetwork:@"ADJgSDK"];
        [ADJgSDK initWithAppId:info[@"app_id"] completionBlock:^(NSError * _Nullable error) {
            if (error){
                [[ATAPI sharedInstance] setInitFlag:0 forNetwork:@"ADJgSDK"];
                NSLog(@"ADJgSDK - 初始化失败%@",error);
            } else {
                
                ATADJgSDKBiddingManager *biddingManage = [ATADJgSDKBiddingManager sharedInstance];
                ATADJgSDKBiddingRequest *request = [ATADJgSDKBiddingRequest new];
                request.unitGroup = unitGroupModel;
                request.placementID = placementModel.placementID;
                request.bidCompletion = completion;
                request.unitID = info[@"slot_id"];
                request.extraInfo = info;
                request.adType = ESCAdFormatReward;
                ADJgSDKRewardVodAdContainer *rewardVodAdContainer = [[ADJgSDKRewardVodAdContainer alloc] initPosId:info[@"slot_id"] delegate:nil];
                request.customObject = rewardVodAdContainer;
                [biddingManage startWithRequestItem:request];
                [rewardVodAdContainer loadRewardVodAd];
                
            }
        }];
        
    }else{
        ATADJgSDKBiddingManager *biddingManage = [ATADJgSDKBiddingManager sharedInstance];
        ATADJgSDKBiddingRequest *request = [ATADJgSDKBiddingRequest new];
        request.unitGroup = unitGroupModel;
        request.placementID = placementModel.placementID;
        request.bidCompletion = completion;
        request.unitID = info[@"slot_id"];
        request.extraInfo = info;
        request.adType = ESCAdFormatReward;
        ADJgSDKRewardVodAdContainer *rewardVodAdContainer = [[ADJgSDKRewardVodAdContainer alloc] initPosId:info[@"slot_id"] delegate:nil];
        request.customObject = rewardVodAdContainer;
        [biddingManage startWithRequestItem:request];
        [rewardVodAdContainer loadRewardVodAd];
    }
    
}

@end
